import numpy as np
from sklearn.datasets import load_iris

# 加载数据集
iris = load_iris()
y = iris.target  # 目标变量（类别标签）

# 计算香农熵
def shannon_entropy(labels):
    _, counts = np.unique(labels, return_counts=True)
    probabilities = counts / len(labels)
    entropy = -np.sum(probabilities * np.log2(probabilities))
    return entropy

# 计算并输出熵值
entropy = shannon_entropy(y)
print(f"数据集的香农熵为：{entropy:.3f}")